package cn.didi.feidi.androidorder.adapter;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.assembly.WaterShowView;
import cn.didi.feidi.androidorder.databinding.ItemMainOrderRecyclerViewBinding;
import cn.didi.feidi.androidorder.model.OrderUser;

/**
 * Created by jinxiong on 2017/8/20.
 */

public class MainOrderItemRecyclerViewAdapter extends
        RecyclerView.Adapter<MainOrderItemRecyclerViewAdapter.ViewHolder> {


    final int status;
    private Callback mCallback;
    private ArrayList<OrderUser> orderUsers;
    private Activity activity;
    private Fragment fragment;
    private OnRootCallback rootCallback;

    public MainOrderItemRecyclerViewAdapter(Fragment fragment, int status) {
        this.status = status;
        this.activity = fragment.getActivity();
        this.fragment = fragment;
        orderUsers = new ArrayList<>();
    }

    public void setRootCallback(OnRootCallback rootCallback) {
        this.rootCallback = rootCallback;
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        ItemMainOrderRecyclerViewBinding binding = DataBindingUtil.inflate(
                activity.getLayoutInflater(), R.layout.item_main_order_recycler_view_, parent, false);

        ViewHolder viewHolder = new ViewHolder(binding.getRoot());
        binding.setAdapter(this);
        viewHolder.setBinding(binding);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if (orderUsers.size() <= position) {
            return;
        }


        OrderUser orderUser = orderUsers.get(position);


        if (status == 4) {

            if (orderUser.getStatusUser() != 4 && orderUser.getStatusUser() != 5) {
                Log.d("jinXiong", "onBindViewHolder: 订单状态不一致");


                return;
            }

        } else {

            if (status != orderUser.getStatusUser()) {
                Log.d("jinXiong", "onBindViewHolder: 订单状态不一致");


                return;
            }
        }

        addListener(holder, position);

        ItemMainOrderRecyclerViewBinding binding = holder.getBinding();
        binding.setOrderUser(orderUsers.get(position));

        binding.mainOrderRecyclerViewItemContainer.removeAllViews();
        List<OrderUser.OrderWaBuOrUsListBean> listBean = orderUser.getOrderWaBuOrUsList();

        if (listBean == null) {
            return;
        }

        int size = listBean.size();
        for (int i = 0; i < size; i++) {
            OrderUser.OrderWaBuOrUsListBean bean = listBean.get(i);
            WaterShowView showView = new WaterShowView(activity);
            showView.setOrUsListBean(bean);
            Glide.with(fragment)
                    .load(bean.getPicUrl())
                    .placeholder(R.drawable.logo)
                    .into(showView.getImageView());

            binding.mainOrderRecyclerViewItemContainer.addView(showView);
        }


    }

    private void addListener(ViewHolder holder, final int position) {

        ItemMainOrderRecyclerViewBinding binding = holder.getBinding();

        final OrderUser orderUser = orderUsers.get(position);

        binding.mainOrderRecyclerViewItemFirstTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mCallback != null) {
                    mCallback.firstButtonClickCallback(orderUser);
                }
            }
        });

        binding.mainOrderRecyclerViewItemSecondTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("jinXiong", "onClick: ");
                if (mCallback != null) {
                    Log.d("jinXiong", "onClick: not");
                    mCallback.secondButtonClickCallback(orderUser);
                }
            }
        });

        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (rootCallback != null) {
                    rootCallback.callback(orderUser);
                }

            }
        });

    }

    public void setOrderUsers(ArrayList<OrderUser> orderUsers) {

        this.orderUsers.clear();

        if (orderUsers != null) {
            this.orderUsers.addAll(orderUsers);
        }

        notifyDataSetChanged();
    }

    public void addOrderUsers(ArrayList<OrderUser> orderUsers) {

        if (orderUsers != null) {
            this.orderUsers.addAll(orderUsers);
            notifyDataSetChanged();
        }

    }

    public void removeOrderUsers(OrderUser orderUser) {

        if (orderUser != null) {

            int size = this.orderUsers.size();
            for (int i = 0; i < size; i++) {

                if (orderUser.getOrderUserId() == orderUsers.get(i).getOrderUserId()) {
                    orderUsers.remove(i);
                    break;
                }
            }
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return orderUsers.size();
    }

    /**
     * @param status
     * @return 根据状态值 返回对应的状态字符串
     */
    public String getStringByStatus(int status) {

        switch (status) {

            case 0: {
                return "等待买家付款";
            }
            case 1: {
                return "等待送水工接单";
            }
            case 2: {
                return "等待买家确认收货";
            }
            case 4: {
                return "交易成功";
            }
            case 5: {
                return "订单已被关闭";
            }
        }
        return "";
    }

    /**
     * @param status
     * @return 第一个textView 的文字根据状态值
     */
    public String getFirstTvStringByStatus(int status) {

        switch (status) {
            case 0: {
                return "取消订单";
            }

            case 2: {
                return "配送信息";
            }

        }
        return "";
    }

    /**
     * @param status
     * @param needExtractDeposit 是否需要补交押金
     * @param commentUserId      是否已经评价了
     * @return
     */
    public String getSecondTvStringByStatus(int status, boolean needExtractDeposit, String commentUserId) {

        switch (status) {

            case 0: {
                return "付款";
            }
            case 1: {
                return "取消订单";
            }
            case 2: {

                if (needExtractDeposit) {
                    return "补交押金";
                } else {
                    return "确认收货";
                }
            }

            case 4: {
                if (commentUserId == null || commentUserId.length() == 0) {
                    return "评价";
                } else {
                    return "查看评价";
                }
            }
            case 5: {
                return "删除订单";
            }

        }
        return "";

    }

    public int getFirstTvVisibilityByStatus(int status) {

        switch (status) {

            case 0: {
                break;
            }

            case 1: {
                return View.GONE;
            }

            case 2: {
                break;
            }

            case 4: {
                return View.GONE;
            }
            case 5: {
                return View.GONE;
            }
        }

        return View.VISIBLE;

    }

    /**
     * 为第二个TextView 设置背景
     *
     * @param status
     * @return
     */
    public Drawable getSecondTvBackground(int status, boolean needExtractDeposit) {

        //@drawable/bg_blue_corner
        switch (status) {


            case 2: {
                if (needExtractDeposit) {
                    return ActivityCompat.getDrawable(activity, R.drawable.bg_red_corner_padding);
                } else {
                    break;
                }
            }


            case 1: {
            }
            case 5: {
                return ActivityCompat.getDrawable(activity, R.drawable.bg_gray_corner);
            }

        }

        return ActivityCompat.getDrawable(activity, R.drawable.bg_blue_corner);

    }

    public
    @ColorInt
    int getSecondTvTextColor(int status, boolean needExtractDeposit) {


        switch (status) {

            case 1: {
                return Color.parseColor("#999999");
            }

            case 2: {

                if (needExtractDeposit) {
                    //补交押金，红色
                    return Color.parseColor("#e92837");
                } else {
                    break;
                }
                //否则就是蓝色
            }

            case 5: {
                return Color.parseColor("#999999");
            }

        }

        return Color.parseColor("#347cf1");

    }

    public interface OnRootCallback {

        void callback(OrderUser orderUser);

    }

    public interface Callback {

        void firstButtonClickCallback(OrderUser orderUser);

        void secondButtonClickCallback(OrderUser orderUser);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ItemMainOrderRecyclerViewBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        public ItemMainOrderRecyclerViewBinding getBinding() {
            return binding;
        }

        public void setBinding(ItemMainOrderRecyclerViewBinding binding) {
            this.binding = binding;
        }
    }

}
